package controllers

import (
	"github.com/astaxie/beego"
	_ "github.com/astaxie/beego/context"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/session"
	"github.com/astaxie/beego/validation"
	"teamcoop/action"
	"teamcoop/global"
	. "teamcoop/models"
)

type IndexController struct {
	beego.Controller
}

func (this *IndexController) Get() {

	var sid, page, email, pwd, nick string
	var class int8
	var err error = nil
	valid := validation.Validation{}
	login := &global.FormLogin{}
	sid = this.Ctx.GetCookie("sid")

	//获取session
	var sessions session.SessionStore
	sessions, err = global.MySessions.GetSessionStore(sid)

	//从session里获取当前登录的用户email
	state := sessions.Get("login_state")
	if "logined" != state || err != nil {
		//未登录
		this.TplNames = "login.html"
		this.Data["error_infoamation"] = ""
		this.Data["error_hide"] = "hide"

		pwd, _ = this.Ctx.GetSecureCookie("jbl's teamcoop", "pwd")
		email = this.Ctx.GetCookie("email")
		beego.Info("GetSecureCookie: email:", email, "\tpwd:", pwd)
		if email != "" && pwd != "" {
			//自动登录
			o := orm.NewOrm()
			user := &User{Email: email, Pwd: pwd}
			err := o.QueryTable("user").Filter("email", email).One(user)
			login.Email = email
			login.Pwd = pwd
			beego.Info("receive:autologin\t", login)
			if err != nil {
				beego.Error("index : QueryTable user fail!")
				goto _error
			}
			_, err = valid.Valid(login)
			if err != nil {
				beego.Error("index : form valid fail!")
				goto _error
			}
			sid, err = action.UserLogin(login.Email, login.Pwd)
			if err != nil {
				beego.Error("index : UserLogin fail!")
				goto _error
			}

			email, nick, class, _ = action.UserInfo(sid)
			this.Ctx.SetCookie("sid", sid)
			this.Ctx.SetCookie("email", login.Email, 1<<31-1, "/")
			this.Ctx.SetSecureCookie("jbl's teamcoop", "pwd", login.Pwd, 1<<31-1, "/")
			this.Ctx.SetCookie("nick", nick)
			this.Redirect("/", 301)
		}
		return
	}
	this.TplNames = "main.html"
	page = this.GetString("page")
	sid = this.Ctx.GetCookie("sid")
	email, nick, class, _ = action.UserInfo(sid)
	this.Data["user_email"] = email
	this.Data["user_nick"] = nick
	this.Data["user_class"] = class

	if page == "" || page == "todo" {
		//TODO页面
		this.Data["IsTodo"] = true
		this.Data["IsLook"] = false
		this.Data["who"] = "UNKONWN"
		who_id, err := this.GetInt("id")
		if err == nil {
			_, nick, _, err = action.UserInfoOther(sid, who_id)
			if err != nil {
				beego.Error(err.Error())
				beego.Error("index : page : todo : UserInfoOther fail!woh_id:", who_id, "\n")
				goto _error
			}
			this.Data["IsLook"] = true
			this.Data["WhoId"] = who_id
		}
		who := this.Ctx.GetCookie("nick")
		if who == nick {
			this.Data["who"] = "我"
		} else {
			this.Data["who"] = nick + " "
		}

	} else if page == "users" {
		//成员管理页面
		this.Data["IsUsers"] = true
	} else if page == "dynamic" {
		//团队动态页面
		this.Data["IsDynamic"] = true
	}
	this.Render()
	beego.Info("receive:main\t", email, "\tpage:", page)
	return

_error:
	if err != nil {
		this.Data["error_hide"] = ""
		this.Data["error_infoamation"] = err.Error()
	}
	return
}
